//
//  VisitorLoginView.swift
//  XYWeibo
//
//  Created by 常晓璇 on 6/23/15.
//  Copyright © 2015 chang xiaoxuan. All rights reserved.
//

// 访客模式
import UIKit
import FFAutoLayout

protocol VisitorLoginViewDelegate : NSObjectProtocol {
    func visitorRegisterButtonClicked()
    func visitorLoginButtonClicked()
}

class VisitorLoginView: UIView {
    
    weak var delegate: VisitorLoginViewDelegate?
    ///  设置视图内容
    ///
    ///  :param: iconName 图标名称
    ///  :param: message  消息文本
    ///  :param: isHome   是否首页
    func setupViewInfo(iconName:String, message:String, isHome:Bool) {
        messageLabel.text = message
        if isHome {
            homeIconView.hidden = false
            iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        } else {
            homeIconView.hidden = true
            iconView.image = UIImage(named: iconName)
            // 将视图层次滞后
            sendSubviewToBack(maskIconView)
        }
    }


    func startAnmiation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        
        anim.repeatCount = MAXFLOAT
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    func register() {
        delegate?.visitorRegisterButtonClicked()
    }
    
    func login() {
        delegate?.visitorLoginButtonClicked()
    
    }
    
    // 视图的默认构造方法
    // 创建控件 并且指定布局 用 xib & sb 开发不会调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    // 解档 用xib & sb 开发会调用
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
       iconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -40))

        maskIconView.ff_Fill(self, insets: UIEdgeInsetsMake(0, 0, 160, 0))
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        homeIconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 0))
        
        messageLabel.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: CGSize(width: 240, height: 40), offset: CGPoint(x: 0, y: 20))
        
        registerButton.ff_AlignVertical(ff_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 20))
        
        loginButton.ff_AlignVertical(ff_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 20))
        
    }
    /// 图标
    lazy var iconView : UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
        }()
    /// 遮盖
    lazy var maskIconView : UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
        }()
    /// 房子
    lazy var homeIconView : UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
        }()
    /// 文字
    lazy var messageLabel : UILabel = {
        let label = UILabel(frame: CGRectMake(0, 0, 240, 40))
        label.text = "关注一些人, 回这里看看有什么惊喜"
        label.tintColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 2
        
        label.sizeToFit()
        return label
        }()
    /// 注册
    lazy var registerButton  : UIButton = {
        let btn = UIButton(frame: CGRectMake(0, 0, 100, 40))
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    /// 登录
    lazy var loginButton : UIButton = {
        let btn = UIButton(frame: CGRectMake(200, 200, 100, 40))
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)

        return btn
        }()

}
